package Template5::Element::Templinsert;
use base qw(Template5::Element::Base);

use strict;
use warnings;

#-------------------------------------------------------------------------------
use Template5::TreeBuilder;
use Template5::Util;
use Template5::Saver;
use Digest::MD5 qw(md5_hex);

#-------------------------------------------------------------------------------
#public
sub new($$$)
{
	my $class    = shift;
	my $htmlnode = shift;
	my $params   = shift;

	my $this = $class->SUPER::new($htmlnode, $params);
	$this->{'type'} = $this->{'htmlnode'}->attr('type') || 'htmltext';
	$this->{'allowed_attrs'} = ['type', 'skip', 'inherited']; 

	return $this;
};

#-------------------------------------------------------------------------------
#public
sub getcontent()
{
	my $this      = shift;	
	my $retVal    = '';
	my $hHTMLNode = $this->SUPER::getstruct();

	if($this->{'type'} eq 'component')
	{
		$retVal = $hHTMLNode->as_text();
	}
	else
	{
		foreach my $hHTMLChildNode($hHTMLNode->content_list())
		{
			if(!ref($hHTMLChildNode))
			{
				$retVal.= $hHTMLChildNode;
				next;
			};
		
			$retVal.= as_HTML($hHTMLChildNode);
		};
	};

	$hHTMLNode->delete();
	my %params = ('skip'      => $this->{'skip'}, 
								'inherited' => $this->{'inherited'},
								'type'      => $this->{'type'});

	return {'content' => $retVal, 'params' => \%params};
};

#private static
sub __calc_content_hash($)
{
	my $node    = shift;
	my $content = '';
	
	foreach my $child ($node->content_list())
	{
		if(ref($child))
		{
			$content .= as_HTML($child);
			
		}
		else
		{
			$content .= $child;
		};
	};
	
	return md5_hex(bytes::substr($content, 0));
};

#public
sub setcontent($$)
{
	my $this    = shift;
	my $content = shift;
	my $params  = shift;
	
	my $retVal = 0;
	 
	if($this->editable())
	{	
		$retVal = 1;
  	my @content;
  	my $internalmd5 = __calc_content_hash($this->{'htmlnode'});
  	my %old_attrs   = ('type' => $this->{'type'}, 'skip' => $this->{'skip'}?1:0, 'inherited' => $this->{'inherited'}?1:0);
  	my $attr_changed = 0;

  	foreach my $key(keys(%{$params}))
  	{
  		if(in_array($key, $this->{'allowed_attrs'}))
  		{
  			$this->{$key} = $params->{$key};
  			
  			if(in_array($key, ['skip', 'inherited']))
  			{
  				if($params->{$key})
  				{
  					$this->{'htmlnode'}->attr($key, $key);
 					}
 					else
 					{
 						$this->{'htmlnode'}->attr($key, undef);
 					};
  			}
  			else
  			{
  				$this->{'htmlnode'}->attr($key, $params->{$key});
 				};
 			};
  	};
  	
  	if($params->{'type'} eq 'component')
  	{
  		@content = (trim($content));	
  	}
  	else
  	{
  		my $treebuilder = Template5::TreeBuilder->new();	
  		@content        = $treebuilder->parse_string($content);
  	};

  	$this->{'htmlnode'}->delete_content();
  	$this->{'htmlnode'}->push_content(@content);

  	foreach my $attrname(keys(%old_attrs)) 
  	{  		
  		if($old_attrs{$attrname} ne $this->{$attrname})
  		{
  			$attr_changed = 1;
  			last;
  		};
  	};

  	if($attr_changed || (__calc_content_hash($this->{'htmlnode'}) ne $internalmd5))
  	{
  		$this->impliciteditable(1);
  	};
  };	
	
	return $retVal;
}; 

#-------------------------------------------------------------------------------
#public
sub getstruct()
{
	my $this   = shift;
	my $retVal = $this->SUPER::getstruct();

	if($this->{'type'} eq 'component')
	{
		$retVal->unshift_content('<![CDATA[');
		$retVal->push_content(']]>');
	};

	$retVal->attr('_saver:element', Template5::Saver->new({'id' => 1, 'type' => 1, 'skip' => 0, 'inherited' => 0, 'nonautoid' => 0, 'inline' => 0, 'notvisible' => 0}));	
	return $retVal;
};
 
1;

